package com.kreedx.duitang;

import com.geccocrawler.gecco.GeccoEngine;
import com.geccocrawler.gecco.listener.EventListener;
import com.geccocrawler.gecco.spring.SpringGeccoEngine;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@EqualsAndHashCode(callSuper = true)
@Component
@Data
public class MyGeccoRunner extends SpringGeccoEngine implements ApplicationRunner {

    @Value("${duitang.url}")
    private String url;

    @Override
    public void init() {
        fetch();
    }

    void fetch(){
        GeccoEngine.create()
                .classpath("com.kreedx.duitang")
                .setEventListener(new EventListener() {
                    @Override
                    public void onStart(GeccoEngine geccoEngine) {

                    }

                    @Override
                    public void onPause(GeccoEngine geccoEngine) {

                    }

                    @Override
                    public void onRestart(GeccoEngine geccoEngine) {

                    }

                    @Override
                    public void onStop(GeccoEngine geccoEngine) {


                    }
                })
                .pipelineFactory(springPipelineFactory)
                .start(url)
                .thread(1)
                .loop(false)
                .start();
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        fetch();
    }
}
